#include "myboard.h"

#include "cbb_event.h"
#include "cbb_init.h"

#include "dev_input.h"
#include "dev_output.h"
#include "dev_uart.h"
#include "dev_st7789.h"

#include "bsp_gpio.h"
#include "bsp_usart1_dma.h"
#include "bsp_usart3_irq.h"
#include "bsp_spi1_dma.h"
#include "device.h"
#include "device_port.h"

#include <string.h>

Device_t* dev_usart1;

Device_t* dev_usart3;

Device_t* dev_btn1;

Device_t* dev_st7789;

Device_t* dev_beep;


void dev_usart_init(void){

    static uint8_t u8Usart1Cache[1032];
    Dev_Uart_t stDevUart1 = {
        .HW = {
            .pfnTransmit = bsp_uart1_dma_transmit,
            .pfnReceive = bsp_uart1_dma_receive,
        },
        .SW = {
            .pu8Data = u8Usart1Cache,
            .u32Len = sizeof(u8Usart1Cache),
            .u32Head = 0,
            .u32Tail = 0,
            .u32Index = 0,
            .u32TimeOut = 5,
        }
    };

    DevDesc_t stDevDesc = {
        .pstDevice = &dev_usart1,
        .pvContext = &stDevUart1,
        .u16Size = DEV_UART_SIZE,
        .u8Type = DEV_TYPE_UART,
    };

    device_open(&stDevDesc);

    static uint8_t u8Usart3Cache[128];
    Dev_Uart_t stDevUart3 = {
        .HW = {
            .pfnTransmit = bsp_usart3_irq_transmit,
            .pfnReceive = bsp_usart3_irq_receive,
        },
        .SW = {
            .pu8Data = u8Usart3Cache,
            .u32Len = sizeof(u8Usart3Cache),
            .u32Head = 0,
            .u32Tail = 0,
            .u32Index = 0,
            .u32TimeOut = 5,
        }
    };

    DevDesc_t stDevDesc1 = {
        .pstDevice = &dev_usart3,
        .pvContext = &stDevUart3,
        .u16Size = DEV_UART_SIZE,
        .u8Type = DEV_TYPE_UART,
    };

    device_open(&stDevDesc1);
}

void dev_beep_init(void){

    Dev_Output_t stDevOutput = {
        .HW = {
            .pfnWrite = bsp_beep_write,
        },
        .SW = {
            .u8State = IO_STATUS_HIGH,
            .u8Active = IO_STATUS_LOW,
        }
    };

    DevDesc_t stDevDesc = {
        .pstDevice = &dev_beep,
        .pvContext = &stDevOutput,
        .u16Size = DEV_OUTPUT_SIZE,
        .u8Type = DEV_TYPE_OUTPUT,
    };

    device_open(&stDevDesc);
}

void dev_button_init(void){
    
    Dev_Input_t stDevInput = {
        .HW = {
            .pfnRead = bsp_button_1_read,
        },
        .SW = {
            .u8Active = IO_STATUS_LOW,
            .u8State = IO_STATUS_HIGH,
        }
    };

    DevDesc_t stDevDesc = {
        .pstDevice = &dev_btn1,
        .pvContext = &stDevInput,
        .u16Size = DEV_INPUT_SIZE,
        .u8Type = DEV_TYPE_INPUT,
    };

    device_open(&stDevDesc);
}

void st7789_init(void){
    
    static uint8_t u8ST7789Cache[240*2];

    Dev_ST7789_t stDevST7789 = {
        .HW = {
            .pfnWriteBL = bsp_lcd_blk_write,
            .pfnWriteCS = bsp_lcd_cs_write,
            .pfnWriteDC = bsp_lcd_dc_write,
            .pfnWriteRST = bsp_lcd_rst_write,
            .pfnSpiTransmit = bsp_spi1_dma_transmit,
        },
        .SW = {
            .u16Height = 240,
            .u16Width = 240,
            .pu8Cache = u8ST7789Cache,
            .u32CacheLen = sizeof(u8ST7789Cache),
        }
    };


    DevDesc_t stDevDesc = {
        .pstDevice = &dev_st7789,
        .pvContext = &stDevST7789,
        .u16Size = DEV_ST7789_SIZE,
        .u8Type = DEV_TYPE_ST7789,
    };

    device_open(&stDevDesc);

    device_ioctl(dev_st7789, ST7789_CMD_INIT, NULL);
}

// void dev_sc7a20h_init(void){
    
// }

Event_t* event_btn1;
Event_t* event_usart3_rx;
Event_t* event_get_dis_task;

uint32_t u32TaskID;

void event_init(void) {

    event_btn1 = cbb_event_register(&(dev_btn1->stEvent.u16Event), EVENT_16, INPUT_EVT_ACTIVE);
    event_usart3_rx = cbb_event_register(&(dev_usart3->stEvent.u16Event), EVENT_16, UART_EVT_DATA_READY);
    event_get_dis_task = cbb_event_register(&u32TaskID, EVENT_32, TASK_GET_DIS_SUCCESS);
}


void myboard_init(void){
    dev_beep_init();
    dev_button_init();
    dev_usart_init();
    st7789_init();
    // dev_sc7a20h_init();

    event_init();
}
INIT_PORT_DEVICE(myboard_init);
